package com.huiren.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.web.service.UploadXML;

import com.huiren.bean.PurOrder;
import com.huiren.mapper.PurOrderMapper;
import com.huiren.service.PurOrderService;
import com.huiren.util.SetUtil;

@Service
public class PurOrderServiceImpl implements PurOrderService {

	@Autowired
	private PurOrderMapper purOrderMapper;

	@Autowired
	private UploadXML webServiceclient;

	public int insert(PurOrder purOrder) throws Exception {
		return purOrderMapper.insert(purOrder);
	}

	@SuppressWarnings("unchecked")
	public List<String> insertFromNet() throws Exception {
		Document document = DocumentHelper.parseText(webServiceclient
				.getPurOrderDataXML("", ""));
		Element root = document.getRootElement();
		Iterator<Element> iterator = root.elementIterator("record");
		List<PurOrder> list = new ArrayList<PurOrder>();
		List<String> ordernumbers = new ArrayList<String>();

		while (iterator.hasNext()) {
			PurOrder purOrder = new PurOrder();
			Element record = iterator.next();
			for (Iterator<Element> iterInner = record.elementIterator(); iterInner
					.hasNext();) {
				Element elementInner = iterInner.next();
				String attname = elementInner.getName();
				String value = elementInner.getText();
				SetUtil.setter(purOrder, attname.toLowerCase(), value,
						String.class);
			}
			list.add(purOrder);
		}
		for (PurOrder purOrder : list) {
			List<PurOrder> purOrderlist = purOrderMapper
					.selectPurOrder(purOrder);
			if (purOrderlist.size() < 1) {
				purOrderMapper.insert(purOrder);
				webServiceclient.updateOrder(purOrder.getOrdernumber());
			}
			ordernumbers.add(purOrder.getOrdernumber());
		}
		return ordernumbers;
	}

	@SuppressWarnings("unchecked")
	public List<String> insertFromFile(String filePath) throws Exception {
		List<String> result = new ArrayList<String>();
		File filelist = new File(filePath);
		List<String> list = Arrays.asList(filelist.list());
		File f = null;
		for (int i = 0; i < list.size(); i++) {
			f = new File(filePath + "/" + list.get(i));
			SAXReader saxReader = new SAXReader();
			Document document;
			try {
				document = saxReader.read(f);
				Element root = document.getRootElement();
				Iterator<Element> iterator = root.elementIterator("record");
				List<PurOrder> purOrderList = new ArrayList<PurOrder>();
				while (iterator.hasNext()) {
					PurOrder purOrder = new PurOrder();
					Element record = iterator.next();
					for (Iterator<Element> iterInner = record.elementIterator(); iterInner
							.hasNext();) {
						Element elementInner = iterInner.next();
						String attname = elementInner.getName();
						String value = elementInner.getText();
						SetUtil.setter(purOrder, attname.toLowerCase(), value,
								String.class);
					}
					purOrderList.add(purOrder);
				}
				int count = 0;
				for (PurOrder purOrder : purOrderList) {
					List<PurOrder> purOrderlist = purOrderMapper
							.selectPurOrder(purOrder);
					count++;
					if (purOrderlist.size() < 1) {
						purOrderMapper.insert(purOrder);
					}
				}

				String strNewPath = "/xmlBak";
				File fnewpath = new File(strNewPath);
				if (!fnewpath.exists())
					fnewpath.mkdirs();
				File fnew = new File(strNewPath + "/" + f.getName());
				f.renameTo(fnew);
				result.add(f.getName() + "文件导入" + count + "条记录成功，并移动到"
						+ fnew.getCanonicalPath() + "\n");
			} catch (DocumentException e) {
				e.printStackTrace();
				result.add(f.getName() + "文件解析失败\n");
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return result;
	}
}
